#pragma
#include <iostream>
#include <string>
#include <fstream>
#include "log.hpp"
#include <unordered_map>
#define FILENAME "./dict.txt"
#define SEP ":"
using std::fstream;
using std::string;
using std::unordered_map;

static void split(const string &str, string *part1, string *part2)
{
    size_t pos = str.find(SEP);
    // size_t pos = 0, size_t len = npos
    // 第一个参数是起始位置，第二个是起始位置往后几个
    *part1 = str.substr(0, pos);
    *part2 = str.substr(pos + 1);
}

class dict
{
public:
    void init()
    {
        fs_.open(FILENAME, std::fstream::in);
        if (!fs_.is_open())
        {
            lg(FATAL, "file open failed!");
        }
        lg(INFO, "file open success");
        string line;
        while (std::getline(fs_, line))
        {
            string part1, part2;
            split(line, &part1, &part2);
            dict_map_.insert(std::make_pair(part1,part2));
        }
        fs_.close();
    }
    string tarnslate(const string& key)
    {
        auto iter = dict_map_.find(key);
        if(iter ==dict_map_.end())
        {
            return "unkonw";
        }
        else
        {
            return iter->second;
        }
    }
private:
    unordered_map<string, string> dict_map_;
    fstream fs_;
};